package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.article.vo.TravelStatisPersistenceVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService{

    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper contentMapper;

    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        return new PageInfo(travelMapper.queryForList(qo));
    }

    @Override
    public Long saveOrUpdate(Travel travel) {

        String content = travel.getTravelContent().getContent();
        //摘要
        if(content.length()>200){
           travel.setSummary(content.substring(0,100));
        }else{
            travel.setSummary(content);
        }

        //最后更新时间
        travel.setLastUpdateTime(new Date());

        if(travel.getId()==null) {
            //创建时间
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);

            //游记内容保存
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            contentMapper.insert(travelContent);
        }else{
            travelMapper.updateByPrimaryKey(travel);

            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            contentMapper.updateByPrimaryKey(travelContent);
        }
        return travel.getId();
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public void changePublicValue(boolean release, Long id) {
        travelMapper.changePublicValue(release, id);
    }

    @Override
    public TravelContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> getViewNumTop3(Long id) {
        return travelMapper.getViewNumTop3(id);
    }

    @Override
    public void updateStatisVO(TravelStatisPersistenceVO vo) {
        travelMapper.updateStatisVO(vo);
    }

    //查收数据库中有多少篇游记
    @Override
    public int selectCount() {
        return travelMapper.selectCount();
    }

    //查询阅读数量的信息
    @Override
    public int selectViewnum() {
        return travelMapper.selectViewnum();
    }

    //查询回复数量的信息
    @Override
    public int selectReplynum() {
        return travelMapper.selectReplynum();
    }

    @Override
    public List<Long> selectAttentionMy(long id) {
        return travelMapper.selectAttentionMy(id);
    }

    @Override
    public void insertUserOrTravel(Long id, Long tid) {
        travelMapper.insertUserOrTravel(id,tid);
    }

    @Override
    public List<Long> getTravelId(Long id) {
        return travelMapper.getTravelId(id);
    }

    @Override
    public List<Travel> getTravelCollect(List<Long> travelId) {
        return travelMapper.getTravelCollect(travelId);
    }
}
